#include <iostream>
#include <fstream>
#include <string>
#include "contact.pb.h"
//using namespace google::protobuf;

using namespace std;

void AddPeopleInfo(s_contacts::PeopleInfo* people)
{
    cout<<"........新增联系人......"<<endl;
    cout<<"联系人姓名：";
    string name;
    getline(cin,name);
    cout<<"联系人生日";
    int birthday;
    cin>>birthday;
    people->set_name(name);
    people->set_birthday(birthday);
    cin.ignore(256, '\n'); 
    for(int i=0;;i++)
    {
        cout<<"联系人电话：";
        string _num;
        getline(cin,_num);
        if(_num.empty()) break;

        s_contacts::PeopleInfo_Phone* phone = people->add_phone();
        phone->set_number(_num);
    }
     cout << "-----------添加联系⼈成功-----------" << endl;
}
int main()
{
    s_contacts::Contacts _con;
    fstream input("../text.bin", ios::in | ios::binary);
    //不存在
    if (!input)
    {
        cout << "contacts.bin not found. Creating a new file." << endl;
    }
    //序列化失败
    else if (!_con.ParseFromIstream(&input))
    {
        cerr << "Failed to parse contacts." << endl;
        input.close();
        return -1;
    }

    AddPeopleInfo(_con.add_contacts());

    // 向磁盘⽂件写⼊新的 contacts
    fstream output("../text.bin", ios::out | ios::trunc | ios::binary);
    if (!_con.SerializeToOstream(&output)) 
    {
        cerr << "Failed to write contacts." << endl;
        input.close();
        output.close();
        return -1;
    }


    return 0;
}